<?php
/*
Plugin Name: Front Page Included-only Categories
Version: 0.2
Plugin URI: 
Description: This version uses a comma separated list of *included* category ids. 
Author: fengyqf
Author URI: http://www.path8.net/
*/ 
global $fpe_menus;

function fpe_active_default_menu($menu, $args){
	global $fpe_menus, $wp_query;
	
	if ( !is_array($menu) ){
		return $menu;
	}
	foreach ( $menu as $key => $item){
		if ( $menu[$key]->current ){
			return $menu;
		}
	}
	if ($wp_query->is_single) {
		$post_category = get_the_category($wp_query->query_vars['p']);
		
		foreach ($menu as $ke => $item){
			foreach ($post_category as $cat_key => $cat_itme) {
				if ($item->object_id === $cat_itme->term_id) {
					$item->current = true;
					$item->classes [] = 'current-menu-item';
					break;
				}
			}
			if ($item->current == true) {
				break;
			}
		}
	}else {
		foreach ($menu as $ke => $item){
			$item->current = true;
			$item->classes [] = 'current-menu-item';
			break;
		}
	}
	
	$fpe_menus = $menu;
	return $menu;
}

add_filter('wp_nav_menu_objects', 'fpe_active_default_menu');

function fpe_filter_post($query){
	global $fpe_menus;
	if ($query->is_home() && $query->is_main_query()){
		wp_nav_menu( array( 'theme_location' => 'primary', 'echo' => false) );
		foreach ( $fpe_menus as $key => $item ) {
			if ($item->current) {
				$query->set ( 'cat', $item->object_id );
			}
		}
		
	} 
}

add_action('pre_get_posts', 'fpe_filter_post');

function fpe_exclude_post($posts){
	global $fpe_menus;
	
	if (WPUF_container::wpuf_is_current_container_page()) {
		return $posts;
	}
	foreach ( $fpe_menus as $key => $item ) {
		if ($item->current) {
			$cat_id = $item->object_id;
			if (isset ( $cat_id )) {
				$foreach_over = false;
				while ( $foreach_over == false ) {
					$foreach_over = true;
					foreach ( $posts as $key => $post ) {
						$post_cat_items = wp_get_post_categories ( $post->ID );
						if (! in_array ( $cat_id, $post_cat_items, false )) {
							array_splice ( $posts, $key, 1 );
							$foreach_over = false;
							break;
						}
					}
				}
			}
		}
	}
	
	return $posts;
}

add_filter('the_posts', 'fpe_exclude_post', 100);

function fpe_once_sticky_category($newvalue) {
	$tmp_array = array ();
	
	foreach ( $newvalue as $key => $item ) {
		$post = get_post ( $item );
		
		$tmp_array [$key] ['id'] = $item;
		$tmp_array [$key] ['cat'] = wp_get_post_categories ( $item );
		$tmp_array [$key] ['last_modify_time'] = strtotime ( $post->post_date_gmt . ' GMT' );
		$tmp_array [$key] ['drop'] = false;
	}
	
	for($kk = 0; $kk < count ( $newvalue ); $kk ++) {
		if ($tmp_array [$kk] ['drop'] == false) {
			for($pp = $kk + 1; $pp < count ( $newvalue ); $pp ++) {
				$arr = array_diff ( $tmp_array [$kk] ['cat'], $tmp_array [$pp] ['cat'] );
				if (empty ( $arr )) {
					if ($tmp_array [$kk] ['last_modify_time'] > $tmp_array [$pp] ['last_modify_time']) {
						$tmp_array [$pp] ['drop'] = true;
					} else {
						$tmp_array [$kk] ['drop'] = true;
					}
				}
			}
		}
	}
	
	for($kk = 0; $kk < count ( $newvalue ); $kk ++) {
		if ($tmp_array [$kk] ['drop'] == true) {
			array_splice ( $newvalue, $kk, 1 );
		}
	}
	return $newvalue;
}

add_filter('pre_update_option_sticky_posts', 'fpe_once_sticky_category');

/*
if ( !function_exists('wp_new_user_notification') ) :

function wp_new_user_notification($user_id, $plaintext_pass = '') {
	$user = get_userdata ( $user_id );

	$user_login = stripslashes ( $user->user_login );
	$user_email = stripslashes ( $user->user_email );

	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
	// we want to reverse this for the plain text arena of emails.
	$blogname = wp_specialchars_decode ( get_option ( 'blogname' ), ENT_QUOTES );

	$message = sprintf ( __ ( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
	$message .= sprintf ( __ ( 'Username: %s' ), $user_login ) . "\r\n\r\n";
	$message .= sprintf ( __ ( 'E-mail: %s' ), $user_email ) . "\r\n";

	@wp_mail ( get_option ( 'admin_email' ), sprintf ( __ ( '[%s] New User Registration' ), $blogname ), $message );

	if (empty ( $plaintext_pass ))
		return;

	$message = home_url().'/register?action=profile'.sprintf ( '&username=%s&userkey=%s', $user_login, $plaintext_pass) . "\r\n";

	wp_mail ( $user_email, sprintf ( __ ( '[%s] Click follow link to active your Account' ), $blogname ), $message );
}

endif;
*/
?>